<?php
/**
 * Created by PhpStorm.
 * User: wumengmeng <wu_mengmeng@foxmail.com>
 * Date: 2019/4/26 0026
 * Time: 14:35
 */

namespace HiCommon\Service;

use HiCommon\MongodbModel\MongoLaravelLog;
use HiCommon\Repository\CheckpointMakeDateRepository;
use HiCommon\Repository\OrderCheckpointRepository;
use HiCommon\Repository\OrderCheckpointVehicleRepository;
use HiCommon\Repository\OrderPayJsonRepository;
use HiCommon\Repository\OrderPayRepository;
use HiCommon\Repository\StoreOrderRepository;
use HiCommon\Repository\UsersRepository;
use HiCommon\Repository\OrderRepairstoreRepository;
use HiCommon\Repository\CouponGrantDetailRepository;
use Illuminate\Support\Facades\DB;
use Modules\Api\Service\StoreOrderService;
use Modules\Api\Service\MaintainShopService;
use HiCommon\Repository\RoadRescueOrderRepository;

class OrderPayService extends BaseService
{

    /**
     * 线上支付
     *
     * @param string $s_pay_type    支付方式：wxmini-微信小程序支付 wxapp-微信app支付 aliapp-支付宝app支付
     * @param string $s_order_type  订单类型：njyy-年检预约单 ptshp-普通商品单 wxfw-维修服务单 wxby-维修保养单
     * @param int    $n_id          订单ID
     * @param int    $n_uid         用户ID
     *
     * @return array|void
     * @author wumengmeng <wu_mengmeng@foxmail.com>
     */
    public function api_online_pay($s_pay_type = '',$s_order_type = '',$n_id = 0,$n_uid = 0){


        if(!in_array($s_pay_type,['wxmini','wxapp','aliapp'])){
            return hello_error('支付方式错误');
        }
        if(!in_array($s_order_type,['njyy','ptshp','wxfw','wxby','dljy'])){
            return hello_error('订单类型错误');
        }
//        $method = $s_pay_type.'_order_'.$s_order_type;
//        $result = OrderPayService::$method($n_id,$n_uid);


        /*微信小程序支付*/
        if($s_pay_type == 'wxmini'){
            if($s_order_type == 'njyy'){
                //年检预约单
                $result = OrderPayService::wxmini_order_njyy($n_id,$n_uid);
            }
            if($s_order_type == 'ptshp'){
                //普通商品单
                $result = OrderPayService::wxmini_order_ptshp($n_id,$n_uid);
            }
            if($s_order_type == 'wxfw'){
                //维修服务单
                $result = OrderPayService::wxmini_order_wxfw($n_id,$n_uid);
            }
            if($s_order_type == 'wxby'){
                //维修保养单
                $result = OrderPayService::wxmini_order_wxby($n_id,$n_uid);
            }
        }

        /*微信app支付*/
        if($s_pay_type == 'wxapp'){
            if($s_order_type == 'njyy'){
                //年检预约单
                $result = OrderPayService::wxapp_order_njyy($n_id,$n_uid);
            }
            if($s_order_type == 'ptshp'){
                //普通商品单
                $result = OrderPayService::wxapp_order_ptshp($n_id,$n_uid);
            }
            if($s_order_type == 'wxfw'){
                //维修服务单
                $result = OrderPayService::wxapp_order_wxfw($n_id,$n_uid);
            }
            if($s_order_type == 'wxby'){
                //维修保养单
                $result = OrderPayService::wxapp_order_wxby($n_id,$n_uid);
            }
            if($s_order_type == 'dljy'){
                //道路救援订单
                $result = OrderPayService::wxapp_order_wxdljy($n_id,$n_uid);
            }
        }

        /*支付宝app支付*/
        if($s_pay_type == 'aliapp') {
            if ($s_order_type == 'njyy') {
                //年检预约单
                $result = OrderPayService::aliapp_order_njyy($n_id, $n_uid);
            }
            if ($s_order_type == 'ptshp') {
                //普通商品单
                $result = OrderPayService::aliapp_order_ptshp($n_id, $n_uid);
            }
            if ($s_order_type == 'wxfw') {
                //维修服务单
                $result = OrderPayService::aliapp_order_wxfw($n_id, $n_uid);
            }
            if ($s_order_type == 'wxby') {
                //保养单
                $result = OrderPayService::aliapp_order_wxby($n_id, $n_uid);
            }
            if($s_order_type == 'dljy'){
                //道路救援订单
                $result = OrderPayService::aliapp_order_dljy($n_id,$n_uid);
            }
        }
        return $result;

//        switch ($s_pay_type)
//        {
//            case 'wxmini':
//                switch ($s_order_type)
//                {
//                    case 'njyy':
//                        break;
//                    case 'njyy':
//                        $result = OrderPayService::wxmini_order_njyy($n_id,$n_uid);
//                        break;
//                    case 'njyy':
//                        $result = OrderPayService::wxmini_order_njyy($n_id,$n_uid);
//                        break;
//                    default:
//                        $result = hello_success('成功');
//                }
//
//                break;
//            default:
//            $result = hello_success('成功');
//        }

    }

    ///年检预约单

    /*年检预约单支付  - [微信小程序支付]*/
    public function wxmini_order_njyy($n_id = 0,$n_uid = 0){
        //获取小程序openid
        $s_openid = UsersRepository::get_small_openid_by_uid($n_uid);
        if(empty($s_openid)){
            return hello_error('用户异常');
        }

        //检测数据是否能支付
        $res = OrderService::api_pay_check_order_checkpoint_for_user($n_uid,$n_id);
        if($res['state'] != 0){
            return $res;
        }

        //微信小程序支付
        $arr_data = $res['data'];
        $s_out_trade_no = $arr_data['out_trade_no'];
        $n_total_fee = $arr_data['total_fee'];
        $s_order_type = 'njyy';
        $s_body = '信达养车-年检预约单支付';
        $result = PayService::api_wxmini_pay($s_order_type,$s_out_trade_no,$n_total_fee,$s_body,$s_openid);

        return  $result;
    }

    /*年检预约单支付  - [微信app支付]*/
    public function wxapp_order_njyy($n_id = 0,$n_uid = 0){
        //检测数据是否能支付
        $res = OrderService::api_pay_check_order_checkpoint_for_user($n_uid,$n_id);
        if($res['state'] != 0){
            return $res;
        }

        //微信小程序支付
        $arr_data = $res['data'];
        $s_out_trade_no = $arr_data['out_trade_no'];
        $n_total_fee = $arr_data['total_fee'];
        $s_order_type = 'njyy';
        $s_body = '信达养车-年检预约单支付';
        $result = PayService::api_wxapp_pay($s_order_type,$s_out_trade_no,$n_total_fee,$s_body);

        return  $result;
    }

    /*年检预约单支付  - [支付宝app支付]*/
    public function aliapp_order_njyy($n_id = 0,$n_uid = 0){

        //检测数据是否能支付
        $res = OrderService::api_pay_check_order_checkpoint_for_user($n_uid,$n_id);
        if($res['state'] != 0){
            return $res;
        }

        //支付宝app支付
        $arr_data = $res['data'];
        $s_out_trade_no = $arr_data['out_trade_no'];
        $n_total_fee = $arr_data['total_fee'];
        $s_order_type = 'njyy';

        $result = PayService::api_aliapp_pay($n_id,$s_order_type,$s_out_trade_no,$n_total_fee);
        return  $result;
    }


    /************************************普通商品订单支付***********************************/

    /*普通商品订单支付  - [微信小程序支付]*/
    public function wxmini_order_ptshp($n_id,$n_uid){
        //获取小程序openid
        $s_openid = UsersRepository::get_small_openid_by_uid($n_uid);
        if(empty($s_openid)){
            return hello_error('用户异常');
        }

        //检测数据是否能支付
        $res = StoreOrderService::check_pay_order_api($n_id, $n_uid);
        if($res['state'] != 0){
            return $res;
        }

        //微信小程序支付
        $arr_data = $res['data'];
        $s_out_trade_no = $arr_data['out_trade_no'];
        $n_total_fee = $arr_data['total_fee'];
        $s_order_type = 'ptshp';
        $s_body = '信达养车-普通商品支付';
        $result = PayService::api_wxmini_pay($s_order_type,$s_out_trade_no,$n_total_fee,$s_body,$s_openid);

        return  $result;
    }

    /*普通商品订单支付  - [微信app支付]*/
    public function wxapp_order_ptshp($n_id,$n_uid){
        //检测数据是否能支付
        $res = StoreOrderService::check_pay_order_api($n_id, $n_uid);
        if ($res['state'] != 0) {
            return $res;
        }

        //微信APP支付
        $arr_data = $res['data'];
        $s_out_trade_no = $arr_data['out_trade_no'];
        $n_total_fee = $arr_data['total_fee'];
        $s_order_type = 'ptshp';
        $s_body = '信达养车-普通商品支付';
        $result = PayService::api_wxapp_pay($s_order_type,$s_out_trade_no,$n_total_fee,$s_body);

        return  $result;
    }

    /*普通商品订单支付  - [支付宝app支付]*/
    public function aliapp_order_ptshp($n_id, $n_uid)
    {
        //检测数据是否能支付
        $res = StoreOrderService::check_pay_order_api($n_id, $n_uid);
        if ($res['state'] != 0) {
            return $res;
        }

        //支付宝app支付
        $arr_data = $res['data'];
        $s_out_trade_no = $arr_data['out_trade_no'];
        $n_total_fee = $arr_data['total_fee'];
        $s_order_type = 'ptshp';

        $result = PayService::api_aliapp_pay($n_id, $s_order_type, $s_out_trade_no, $n_total_fee);
        return $result;
    }

    /************************************维修服务订单支付***********************************/

    /*维修服务订单支付  - [微信小程序支付]*/
    public function wxmini_order_wxfw($n_id,$n_uid){
        //获取小程序openid
        $s_openid = UsersRepository::get_small_openid_by_uid($n_uid);
        if(empty($s_openid)){
            return hello_error('用户异常');
        }

        //检测数据是否能支付
        $res = MaintainShopService::check_wxfw_pay_order_api($n_id, $n_uid);
        if($res['state'] != 0){
            return $res;
        }

        //微信小程序支付
        $arr_data = $res['data'];
        $s_out_trade_no = $arr_data['out_trade_no'];
        $n_total_fee = $arr_data['total_fee'];
        $s_order_type = 'wxfw';
        $s_body = '信达养车-服务订单支付';
        $result = PayService::api_wxmini_pay($s_order_type,$s_out_trade_no,$n_total_fee,$s_body,$s_openid);

        return  $result;
    }

    /*维修服务订单支付  - [微信app支付]*/
    public function wxapp_order_wxfw($n_id,$n_uid){
        //检测数据是否能支付
        $res = MaintainShopService::check_wxfw_pay_order_api($n_id, $n_uid);
        if ($res['state'] != 0) {
            return $res;
        }

        //微信APP支付
        $arr_data = $res['data'];
        $s_out_trade_no = $arr_data['out_trade_no'];
        $n_total_fee = $arr_data['total_fee'];
        $s_order_type = 'wxfw';
        $s_body = '信达养车-服务订单支付';
        $result = PayService::api_wxapp_pay($s_order_type,$s_out_trade_no,$n_total_fee,$s_body);

        return  $result;
    }

    /*维修服务订单支付  - [支付宝app支付]*/
    public function aliapp_order_wxfw($n_id, $n_uid){
        //检测数据是否能支付
        $res = MaintainShopService::check_wxfw_pay_order_api($n_id, $n_uid);
        if ($res['state'] != 0) {
            return $res;
        }

        //支付宝app支付
        $arr_data = $res['data'];
        $s_out_trade_no = $arr_data['out_trade_no'];
        $n_total_fee = $arr_data['total_fee'];
        $s_order_type = 'wxfw';

        $result = PayService::api_aliapp_pay($n_id, $s_order_type, $s_out_trade_no, $n_total_fee);
        return $result;
    }

    /************************************维修保养订单支付***********************************/

    /*维修保养订单支付  - [微信小程序支付]*/
    public function wxmini_order_wxby($n_id,$n_uid){
        //获取小程序openid
        $s_openid = UsersRepository::get_small_openid_by_uid($n_uid);
        if(empty($s_openid)){
            return hello_error('用户异常');
        }

        //检测数据是否能支付
        $res = MaintainShopService::check_wxfw_pay_order_api($n_id, $n_uid);
        if($res['state'] != 0){
            return $res;
        }

        //微信小程序支付
        $arr_data = $res['data'];
        $s_out_trade_no = $arr_data['out_trade_no'];
        $n_total_fee = $arr_data['total_fee'];
        $s_order_type = 'wxby';
        $s_body = '信达养车-保养订单支付';
        $result = PayService::api_wxmini_pay($s_order_type,$s_out_trade_no,$n_total_fee,$s_body,$s_openid);

        return  $result;
    }

    /*维修保养订单支付  - [微信app支付]*/
    public function wxapp_order_wxby($n_id, $n_uid){
        //检测数据是否能支付
        $res = MaintainShopService::check_wxfw_pay_order_api($n_id, $n_uid);
        if ($res['state'] != 0) {
            return $res;
        }

        //微信APP支付
        $arr_data = $res['data'];
        $s_out_trade_no = $arr_data['out_trade_no'];
        $n_total_fee = $arr_data['total_fee'];
        $s_order_type = 'wxby';
        $s_body = '信达养车-养订单支付';
        $result = PayService::api_wxapp_pay($s_order_type,$s_out_trade_no,$n_total_fee,$s_body);

        return  $result;
    }

    /* 道路救援订单支付  【微信APP支付】*/
    public function wxapp_order_wxdljy( $n_id , $n_uid ){
        //获取数据的详情road_rescue_order
        $arr_option['field'] = ['id','price','pay_status','status','order_sn', 'created_at' ];
        $res = RoadRescueOrderRepository::get_one( $n_id , $arr_option);
        if( empty( $res->first() ) ){
            return hello_error('订单错误');
        }
        $data = $res->toarray();

        //订单下单时间与支付时间不能超过15分钟
        $created_time = strtotime( $data['created_at'] );
        //当前时间 - 下单的时间 > 15 * 60 (15分钟)
        if( time() - $created_time > 15 * 60 ){

            $arr_data['id'] = $data['id'];
            $arr_data['pay_status'] = 2;
            $arr_data['status'] = 4;
            RoadRescueOrderRepository::edit_one( (array) $arr_data );   //将订单修改为过期状态
            return hello_error('订单已过期，请重新下单');
        }

        if( $data['pay_status']['id'] == 1 ){
            return hello_error('订单已支付');
        }

        //微信APP支付
        $s_out_trade_no = $data['order_sn'];
        $n_total_fee = $data['price'] * 100;
        $s_order_type = 'dljy';
        $s_body = '信达养车-道路维修订单支付';
        $result = PayService::api_wxapp_pay($s_order_type,$s_out_trade_no,$n_total_fee,$s_body);

        return  $result;
    }

    /* 道路救援订单支付  【支付宝APP支付】*/
    public function aliapp_order_dljy( $n_id, $n_uid ){
        //获取数据的详情road_rescue_order
        $arr_option['field'] = ['id','price','pay_status','status','order_sn'];
        $res = RoadRescueOrderRepository::get_one( $n_id , $arr_option);



        if( empty( $res->first() ) ){
            return hello_error('订单错误');
        }

        $data = $res->toarray();
        if( $data['pay_status']['id'] != 2 ){
            return hello_error('订单已支付');
        }
        //支付宝app支付
//        $arr_data = $res['data'];
        $s_out_trade_no = $data['order_sn'];
        $n_total_fee = $data['price'] * 100;
        $s_order_type = 'dljy';

        $result = PayService::api_aliapp_pay($n_id, $s_order_type, $s_out_trade_no, $n_total_fee);

        return  $result;

    }




    /*维修保养订单支付  - [支付宝app支付]*/
    public function aliapp_order_wxby($n_id, $n_uid){
        //检测数据是否能支付
        $res = MaintainShopService::check_wxfw_pay_order_api($n_id, $n_uid);
        if ($res['state'] != 0) {
            return $res;
        }

        //支付宝app支付
        $arr_data = $res['data'];
        $s_out_trade_no = $arr_data['out_trade_no'];
        $n_total_fee = $arr_data['total_fee'];
        $s_order_type = 'wxby';

        $result = PayService::api_aliapp_pay($n_id, $s_order_type, $s_out_trade_no, $n_total_fee);
        return $result;
    }























    /*微信支付结果回调确认*/
    public function api_notify_confirm_for_wechat_pay($arr_data){
        if(empty($arr_data)){
            return hello_error('数据不能为空');
        }
        if(!(gettype($arr_data) == 'array')){
            $arr_data = json_decode($arr_data,true);
        }

        $arr_attach = explode('-',$arr_data['attach']);
        $s_order_type = $arr_attach[1];//订单类型：njyy-年检预约单 ptshp-普通商品单 wxfw-维修服务单 wxby-维修保养单 dljy-道路救援
        $s_pay_type = $arr_attach[2];//支付方式：wxmini-微信小程序支付 wxapp-微信app支付 aliapp-支付宝app支付
        $s_order_sn = $arr_attach[3];//订单编号
        $s_pay_time = ymdhis_to_time_date($arr_data['time_end']);//支付时间
        $s_transaction_id = $arr_data['transaction_id'];//支付交易号（支付宝或微信）
        $n_total_fee = $arr_data['total_fee'];//本次交易支付的订单金额：单位分
        $n_cash_fee = $arr_data['cash_fee'];//用户在交易中支付的金额：单位分

        //支付回调
        $result = OrderPayService::notify_for_all($s_order_type, $s_pay_type, $s_order_sn, $s_pay_time,$s_transaction_id,$n_total_fee,$n_cash_fee,$arr_data);
        return $result;
    }

    /*支付宝支付结果回调*/
    public function api_notify_confirm_for_ali_pay($arr_data){
        $trade_status = $arr_data['trade_status'];

        //支付成功
        if($trade_status == 'TRADE_SUCCESS'){
            $arr_body = json_decode($arr_data['body'],true);
            $s_order_type = $arr_body['order_type'];//订单类型：njyy-年检预约单 ptshp-普通商品单 wxfw-维修服务单 wxby-维修保养单
            $s_pay_type = $arr_body['pay_type'];//支付方式：wxmini-微信小程序支付 wxapp-微信app支付 aliapp-支付宝app支付
            $s_order_sn = $arr_data['out_trade_no'];//订单编号
            $s_pay_time = $arr_data['gmt_payment'];//支付时间
//            $s_out_trade_no = $arr_data['out_trade_no'];
            $s_transaction_id = $arr_data['trade_no'];//支付交易号（支付宝或微信）
            $n_total_fee = intval($arr_data['total_amount'] * 100);//本次交易支付的订单金额：单位分
            $n_cash_fee = intval($arr_data['buyer_pay_amount'] * 100);//用户在交易中支付的金额：单位分

            //支付回调
            $result = OrderPayService::notify_for_all($s_order_type, $s_pay_type, $s_order_sn, $s_pay_time,$s_transaction_id,$n_total_fee,$n_cash_fee,$arr_data);


        }
        else{
            $result =  hello_error('失败',[],'订单交易状态异常');
        }


        return $result;
    }

    /**
     *
     * 回调中转站
     *
     * @param string $s_order_type          订单类型：njyy-年检预约单 ptshp-普通商品单 wxfw-维修服务单 wxby-维修保养单
     * @param string $s_pay_type            支付方式：wxmini-微信小程序支付 wxapp-微信app支付 aliapp-支付宝app支付
     * @param string $s_order_sn            订单编号
     * @param null   $s_pay_time            支付时间
     * @param string $s_transaction_id      支付交易号（支付宝或微信）
     * @param int    $n_total_fee           本次交易支付的订单金额：单位分
     * @param int    $n_cash_fee            用户在交易中支付的金额：单位分
     * @param array  $arr_data              支付回调参数集合
     *
     * @return array|void
     * @author wumengmeng <wu_mengmeng@foxmail.com>
     */
    public function notify_for_all($s_order_type = '',$s_pay_type = '',$s_order_sn = '',$s_pay_time = null,$s_transaction_id = '',$n_total_fee = 0,$n_cash_fee = 0,$arr_data = []){

        //支付回调-修改订单状态
        if($s_order_type == 'njyy'){
            $result = OrderPayService::notify_for_njyy($s_order_sn,$s_pay_type,$s_pay_time);
        }
        elseif($s_order_type == 'ptshp'){
            //普通商品单
            $result = OrderPayService::notify_for_ptshp($s_order_sn, $s_transaction_id, $s_pay_time, $s_pay_type);
        }
        elseif($s_order_type == 'wxfw'){
            //维修服务单
            $result = OrderPayService::notify_for_wxfw($s_order_sn, $s_transaction_id, $s_pay_time, $s_pay_type);
        }
        elseif($s_order_type == 'wxby'){
            //维修保养单
            $result = OrderPayService::notify_for_wxby($s_order_sn, $s_transaction_id, $s_pay_time, $s_pay_type);
        }
        elseif($s_order_type == 'dljy'){
            //道路救援订单
            $result = OrderPayService::notify_for_dljy($s_order_sn, $s_transaction_id, $s_pay_time, $s_pay_type);
        }
        else{
            $result =  hello_error('失败',[],'订单类型异常');
        }

        //支付回调-记录支付信息
        if($result['state'] == 0){

            $result = OrderPayService::record_online_pay($s_order_sn,$s_order_type,$s_pay_type,$s_transaction_id,$n_total_fee,$n_cash_fee,$s_pay_time,$arr_data);

        }


        return $result;
    }

    /**
     * 年检预约订单支付回调
     *
     * @param string $s_order_sn
     * @param string $s_pay_type
     * @param null   $s_pay_time
     *
     * @return array
     * @author wumengmeng <wu_mengmeng@foxmail.com>
     */
    public function notify_for_njyy($s_order_sn = '', $s_pay_type = '', $s_pay_time = null){
        //确认支付 + 预约成功 + 支付时间

        //主订单修改状态
        $arr_where = ['order_sn'=>$s_order_sn];
        $arr_data = [
            'payment_method'=>$s_pay_type,
            'appointment_state'=>2,
            'pay_state'=>2,
            'complete_state'=>-1,
//            'refund_state'=>0,
//            'check_state'=>1,
//            'appraise_state'=>0,
            'pay_time'=>$s_pay_time,
        ];
        $res = OrderCheckpointRepository::edit_by_where($arr_where,$arr_data);
        if($res === false){
            return hello_error('确认失败',[],'支付状态确认失败');
        }

        $res = OrderCheckpointRepository::get_one_by_where($arr_where,['field'=>['id']]);
        if(is_null($res)){
            return hello_error('确认失败',[],'没有查询到需要支付数据');
        }
        $arr_order_checkpoint = $res->toarray();

        //子表状态处理
        $arr_where = ['order_checkpoint_id'=>$arr_order_checkpoint['id']];
        $arr_data = [
            'check_state'=>1,
        ];
        $res = OrderCheckpointVehicleRepository::edit_by_where($arr_where,$arr_data);
        if($res === false){
            return hello_error('确认失败',[],'子表状态修改失败');
        }
        return  hello_success('成功');
    }

    /*普通商品订单支付回调*/
    public function notify_for_ptshp($s_order_sn = '', $s_transaction_id = '', $s_pay_time = null, $s_pay_type)
    {
        //订单状态修改
        $arr_where = ['order_no'=>$s_order_sn];
        $arr_data = [
            'pay_status' => 2,
            'pay_type' => $s_pay_type == 'aliapp' ? 2 : 1,
            'outer_trade_no' => $s_transaction_id,
            'pay_time' => $s_pay_time,
        ];
        $res = StoreOrderRepository::edit_one($arr_where, $arr_data);

        if($res === false){
            return hello_error('确认失败',[],'订单状态修改失败');
        }

        return  hello_success('成功');
    }

    /*维修服务订单支付回调*/
    public function notify_for_wxfw($s_order_sn, $s_transaction_id, $s_pay_time, $s_pay_type)
    {
        //订单状态修改
        $arr_where = ['order_number' => $s_order_sn];
        $arr_data = [
            'pay_status' => 1,
            'pay_type' => $s_pay_type == 'aliapp' ? 2 : 1,
            'pay_time' => $s_pay_time,
        ];

        $res = OrderRepairstoreRepository::edit_pay_status($arr_where, $arr_data);

        if ($res == 1) {
            return hello_success('成功');
        }

        return hello_error('确认失败', [], '订单状态修改失败');
    }


    /*道路救援订单支付回调*/
    public function notify_for_dljy( $s_order_sn, $s_transaction_id, $s_pay_time, $s_pay_type ){
        //订单状态修改
        $arr_where = ['order_sn' => $s_order_sn];
        $arr_data = [
            'pay_status' => 1,
            'pay_type' => $s_pay_type == 'aliapp' ? 2 : 1,
            'pay_time' => $s_pay_time,
            'status' => 1,
        ];
        $res = RoadRescueOrderRepository::edit_one_where($arr_where, $arr_data);
        if ($res == 1) {
            return hello_success('成功');
        }
        return hello_error('确认失败', [], '订单状态修改失败');
    }


    /*维修保养订单支付回调*/
    public function notify_for_wxby($s_order_sn, $s_transaction_id, $s_pay_time, $s_pay_type){
        //订单状态修改
        $arr_where = ['order_number' => $s_order_sn];
        $arr_data = [
            'pay_status' => 1,
            'pay_type' => $s_pay_type == 'aliapp' ? 2 : 1,
            'pay_time' => $s_pay_time,
        ];

        $res = OrderRepairstoreRepository::edit_pay_status($arr_where, $arr_data);

        if ($res == 1) {
            return hello_success('成功');
        }

        return hello_error('确认失败', [], '订单状态修改失败');
    }

    /**
     * 维修店修改订单为已支付
     *
     * @param $s_order_sn
     *
     * @return array
     * @author wumengmeng <wu_mengmeng@foxmail.com>
     */
    public function notify_wechat_for_order_repairstore($s_order_sn, $pay_price)
    {
        $map = ['order_number' => $s_order_sn];

        $upData = [
            'pay_status' => 1,
            'pay_price' => $pay_price,
            'pay_time' => date('Y-m-d H:i:s', time()),
        ];
        $res = OrderRepairstoreRepository::edit_pay_status((array)$map, (array)$upData);

        //获取用户所用的优惠券id
        $coupon = OrderRepairstoreRepository::get_one_by_where((array)$map, ['field' => ['user_coupons_id', 'shop_id']]);

        if (!empty($coupon->first()) && $coupon->user_coupons_id) {
            $up_data = ['id' => $coupon->user_coupons_id, 'status' => 2, 'repairstore_id' => $coupon->shop_id];
            CouponGrantDetailRepository::edit_one($up_data);
        }

        if ($res) {
            return hello_success('成功');
        }
        return hello_error('确认失败-1');
    }

    /**
     * 记录支付信息
     *
     * @param string $s_order_sn
     * @param string $s_order_type
     * @param string $s_pay_type
     * @param string $s_transaction_id
     * @param int    $n_total_fee
     * @param int    $n_cash_fee
     * @param null   $s_pay_time
     * @param array  $arr_data
     *
     * @return array
     * @author wumengmeng <wu_mengmeng@foxmail.com>
     */
    public function record_online_pay($s_order_sn = '',$s_order_type = '',$s_pay_type = '',$s_transaction_id = '',$n_total_fee = 0,$n_cash_fee = 0,$s_pay_time = null,$arr_data = []){
        $arr_pay_data = [
          'order_sn'=>$s_order_sn,
          'out_trade_no'=>$s_order_sn,
          'transaction_id'=>$s_transaction_id,
          'pay_time'=>$s_pay_time,
          'total_fee'=>$n_total_fee,
          'pay_fee'=>$n_cash_fee,
          'pay_type'=>$s_pay_type,
          'order_type'=>$s_order_type,
        ];
        $res = OrderPayRepository::base_add_one($arr_pay_data)->toarray();
        if(empty($res)){
            return hello_error('支付表记录失败');
        }

        $n_order_pay_id = $res['id'];
        $arr_payjson_data = [
          'order_pay_id'=>$n_order_pay_id,
          'json_data'=>json_encode($arr_data),

        ];
        $res = OrderPayJsonRepository::base_add_one($arr_payjson_data)->toarray();
        if(empty($res)){
            return hello_error('微信支付表记录失败',[],'添加详细信息失败');
        }

        /*通知队列-塞入订单支付成功通知*/
        queue_in_notice_order_pay_success($s_order_sn,$s_order_type);

        return hello_success('支付表记录成功');

    }


    /**
     * 订单支付结果查询
     *
     * @param int    $n_id
     * @param string $s_order_type
     *
     * @return array
     * @author wumengmeng <wu_mengmeng@foxmail.com>
     */
    public function api_order_result($n_id = 0,$s_order_type = ''){
        $n_id = intval($n_id);
        if($n_id <= 0){
            return hello_error('订单ID不能为空');
        }

        if(!in_array($s_order_type,['njyy','ptshp','wxfw','wxby','dljy'])){
            return hello_error('订单类型错误');
        }


        /*【特别注意】返回格式要统一 id为订单ID pay_state为订单支付状态（1未支付 2已支付）*/
        //array:4 [
        //  "state" => 0
        //  "msg" => "成功"
        //  "data" => array:2 [
        //    "id" => 241
        //    "pay_state" => 2
        //  ]
        //  "error" => ""
        //]
        if($s_order_type == 'njyy'){
            //年检预约单-支付结果查询
            $result = OrderPayService::order_result_for_njyy($n_id);
        }
        elseif($s_order_type == 'ptshp'){
            //普通商品订单-支付结果查询
            $result = OrderPayService::order_result_for_ptshp($n_id);
        }
        elseif($s_order_type == 'wxfw'){
            //维修服务-支付结果查询
            $result = OrderPayService::order_result_for_wxfw($n_id);
        }
        elseif($s_order_type == 'wxby'){
            //维修保养-支付结果查询
            $result = OrderPayService::order_result_for_wxby($n_id);
        }
        elseif($s_order_type == 'dljy'){
            //维修保养-支付结果查询
            $result = OrderPayService::order_result_for_dljy($n_id);
        }
        else{
            $result =  hello_error('失败',[],'订单类型异常');
        }

        return $result;
    }

    /*年检预约单-支付结果查询*/
    public function order_result_for_njyy($n_id){

        $arr_option = [
          'field'=>['id','pay_state']
        ];
        $result = OrderCheckpointRepository::base_find_one($n_id,$arr_option);
        if(is_null($result)){
            return hello_error('数据不存在');
        }
        $result = $result->toarray();
        
        return  hello_success('成功',$result);
    }

    /**
     * 普通商品支付完成后-查询订单
     */
    public function order_result_for_ptshp($n_id)
    {
        try {
            if ($n_id <= 0) {
                return hello_error('数据id不能为空');
            }
            $arr_option = [
                'field' => ['id', 'pay_status']
            ];
            $result = StoreOrderRepository::get_one_by_where(['id' => $n_id], $arr_option);
            if (is_null($result)) {
                return hello_error('数据不存在');
            }
            $result = [
                'id' => $result->id,
                'pay_state' => $result->pay_status,
            ];

            return hello_success('成功', $result);
        } catch (\Exception $exception) {
            return hello_error('失败', [], $exception->getMessage());
        }
    }

    /**
     * 维修服务支付完成后-查询订单
     */
    public function order_result_for_wxfw($n_id)
    {
        try {
            if ($n_id <= 0) {
                return hello_error('数据id不能为空');
            }
            $arr_option = [
                'field' => ['id', 'pay_status']
            ];
            $result = OrderRepairstoreRepository::get_one_by_where(['id' => $n_id], $arr_option);
            if (is_null($result)) {
                return hello_error('数据不存在');
            }

            $result = [
                'id' => $result->id,
                'pay_state' => intval($result->pay_status) + 1,
            ];

            return hello_success('成功', $result);
        } catch (\Exception $exception) {
            return hello_error('失败', [], $exception->getMessage());
        }
    }

    /**
     * 维修保养支付完成后-查询订单
     */
    public function order_result_for_wxby($n_id)
    {
        try {
            if ($n_id <= 0) {
                return hello_error('数据id不能为空');
            }
            $arr_option = [
                'field' => ['id', 'pay_status']
            ];
            $result = OrderRepairstoreRepository::get_one_by_where(['id' => $n_id], $arr_option);
            if (is_null($result)) {
                return hello_error('数据不存在');
            }

            $result = [
                'id' => $result->id,
                'pay_state' => $result->pay_status + 1,
            ];

            return hello_success('成功', $result);
        } catch (\Exception $exception) {
            return hello_error('失败', [], $exception->getMessage());
        }
    }


    /**
     * 维修保养支付完成后-查询订单
     */
    public function order_result_for_dljy($n_id)
    {
        try {
            if ($n_id <= 0) {
                return hello_error('数据id不能为空');
            }
            $arr_option = [
                'field' => ['id', 'pay_status']
            ];
            $result = RoadRescueOrderRepository::get_one( $n_id , $arr_option);
            if (is_null($result)) {
                return hello_error('数据不存在');
            }

            $result = [
                'id' => $result->id,
                'pay_state' => 2,  //订单支付成功 返回2
            ];

            return hello_success('成功', $result);
        } catch (\Exception $exception) {
            return hello_error('失败', [], $exception->getMessage());
        }
    }




//    public function mini_pay($n_id){
//        $s_out_trade_no = random_order_sn();
//        $s_openid = 'o3Fu55bADEeeAx11EHQsmOPcFarI';
//        $n_total_fee = 1;// **单位：分**
//        $s_body = 'test body - 测试1';
//        $s_attach = 'order_checkpoint';
//        //查询订单信息
//        //判断订单状态
//        //判断时段是否已满状态
//        $result = PayYanSongDaService::mini_app_pay($s_out_trade_no, $s_openid, $n_total_fee, $s_body, $s_attach);
//        return  $result;
//    }





}